package com.x.provider.customer.factory.dto.impl;

import com.x.provider.api.customer.enums.CustomerOptions;
import com.x.provider.api.customer.enums.CustomerRelationEnum;
import com.x.provider.api.customer.model.dto.customer.CustomerDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerListReqDTO;
import com.x.provider.customer.factory.dto.CustomerDTOBuilder;
import com.x.provider.customer.service.customer.CustomerRelationService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author: liushenyi
 * @date: 2022/07/25/16:04
 */
@Service("customerRelationBuilder")
public class CustomerRelationBuilderImpl implements CustomerDTOBuilder {

   private final CustomerRelationService customerRelationService;

   public CustomerRelationBuilderImpl(CustomerRelationService customerRelationService){
      this.customerRelationService = customerRelationService;
   }

   @Override
   public void build(CustomerListReqDTO listCustomerAO, Map<Long, CustomerDTO> dest) {
      if (!listCustomerAO.getCustomerOptions().contains(CustomerOptions.CUSTOMER_RELATION.name())){
         return;
      }
      if ( listCustomerAO.getSessionCustomerId() == null){
         dest.entrySet().forEach(item -> {
            item.getValue().setCustomerRelation(CustomerRelationEnum.NO_RELATION.getValue());
         });
         return;
      }
      final Map<Long, Integer> customerRelation = customerRelationService.listCustomerRelation(listCustomerAO.getSessionCustomerId(), listCustomerAO.getCustomerIds());
      dest.entrySet().forEach(item -> {
         item.getValue().setCustomerRelation(customerRelation.getOrDefault(item.getKey(), CustomerRelationEnum.NO_RELATION.getValue()));
      });
   }
}
